<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售单号" prop="saleOrderNum">
        <el-input
          v-model="queryParams.saleOrderNum"
          placeholder="请输入销售单号"
          clearable
          maxlength="50"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货单号" prop="shipmentNum">
        <el-input
          v-model="queryParams.shipmentNum"
          placeholder="请输入发货单号"
          clearable
          maxlength="50"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select
          v-model="queryParams.customerId"
          placeholder="请选择客户"
          clearable
          filterable
          size="small"
          style="width: 100%"
        >
          <el-option
            v-for="dict in customerList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收款方式" prop="paymentMethod">
        <el-select v-model="queryParams.paymentMethod" placeholder="请选择收款方式" style="width: 100%" clearable>
          <el-option
            v-for="dict in dict.type.erp_payment_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="invoiceList">
      <el-table-column label="发货单号" align="center" prop="shipmentNum"/>
      <el-table-column label="销售单号" align="center" prop="saleOrderNum"/>
      <el-table-column label="客户" align="center" prop="customerName"/>
      <el-table-column label="客户电话" align="center" prop="customerPhone"/>
      <el-table-column label="销售总额" align="center" prop="saleBonus"/>
      <el-table-column label="收款方式" align="center" prop="paymentMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_payment_type" :value="scope.row.paymentMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_process_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="是否退货" align="center" prop="isReturn">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_return_goods" :value="scope.row.isReturn"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
          >查看/确认
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            v-show="scope.row.isWarehousing != 0 && scope.row.type != 3"
            @click="returnGoods(scope.row)"
          >退货
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listInvoice} from "@/api/erp/invoice";
import {listCustomer} from "@/api/erp/customer";

export default {
  name: "Invoice",
  dicts: ['erp_process_type', 'erp_return_goods', 'erp_payment_type'],
  data() {
    return {
      //客户列表
      customerList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售发货单表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleOrderNum: null,
        shipmentNum: null,
        saleId: null,
        customerId: null,
        paymentMethod: null,
        customerPhone: null,
        fixedTelephone: null,
        saleBonus: null,
        type: null,
        uodateTime: null,
        createUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.selectCustomer();
  },
  activated() {
    this.getList();
  },
  methods: {

    //查询客户列表
    selectCustomer() {
      listCustomer().then(response => {
        this.customerList = response.rows;
      });
    },

    //退货按操作
    returnGoods(row) {
      this.$router.push({path: '/returnGoods', query: {id: row.saleId}});
    },
    //查看详情按钮操作
    handleInfo(row) {
      this.$router.push({path: '/invoiceInfo', query: {id: row.saleId}});
    },

    /** 查询销售发货单列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        saleOrderNum: null,
        shipmentNum: null,
        saleId: null,
        customerId: null,
        paymentMethod: null,
        customerPhone: null,
        fixedTelephone: null,
        saleBonus: null,
        type: null,
        createTime: null,
        uodateTime: null,
        createUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

  }
};
</script>
